package ch6.pool;

import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author LuHuanCheng
 * 类说明：演示定时线程池的使用
 */
public class UseScheduledThreadPoolExecutor {
    public static void main(String[] args) {
        ScheduledThreadPoolExecutor schedule = new ScheduledThreadPoolExecutor(1);

        //延时Runnable任务（只执行一次）
        schedule.schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println("任务只执行一次");
            }
        }, 3, TimeUnit.SECONDS);


        //固定延时间隔执行任务
        schedule.scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
                System.out.println("固定延时间隔");
            }
        }, 1, 1, TimeUnit.SECONDS);


        //固定时间间隔执行任务
        schedule.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                System.out.println("固定时间间隔");
            }
        }, 1, 1, TimeUnit.SECONDS);
    }
}